class Solution {
public:
        unordered_map<Node*, Node*> cachedNode;
    Node* copyRandomList(Node* head) {
        if(head==nullptr) return nullptr;
        
        if(!cachedNode.count(head))
        {
            Node*newhead=new Node(head->val);
            cachedNode[head]=newhead;
            newhead->next=copyRandomList(head->next);
            newhead->random=copyRandomList(head->random);
        }

        return cachedNode[head];
        
    }
};